#import "ChatsView.h"
#import "SolarSeekEngine.h"
#import "SolarSeekApplication.h"
#import "ChatsController.h"
#import "Chats.h"
#import "ChatView.h"
#import "Peers.h"
#import "ChatElements.h"
#import "ApplicationDefaults.h"

@interface ChatsView (Saving)
- (void)saveChats;
@end

@implementation ChatsView

- (id<SourceListElement>)element { return _element; }

- (void)awakeFromNib
{
	_element = [[ChatsElement alloc] initWithView:self expandable:NO];
	[nibTableRooms setTarget:self];
	[nibTableRooms setDoubleAction:@selector(joinRoom:)];
	
	[nibTableUsers setTarget:self];
	[nibTableUsers setDoubleAction:@selector(startPrivateChats:)];
	
	NSArray *a = [[NSUserDefaults standardUserDefaults] stringArrayForKey:SolarSeekDefaultChats];
	[self performSelector:@selector(autojoinRooms:) withObject:a afterDelay:0.5];
}

- (void)dealloc {
	[_element release];
	[super dealloc];
}

- (void)autojoinRooms:(NSArray *)rooms {
	NSEnumerator *e = [rooms objectEnumerator];
	NSString *roomname;
	while(roomname = [e nextObject]) {
		[[[NSApp engine] chatsController] chatWithRoom:[[[NSApp engine] chatsController] roomNamed:roomname] autojoin:YES];
	}
}

- (IBAction)joinRoom:(id)sender
{
	NSEnumerator *e = [[nibControllerConversations selectedObjects] objectEnumerator];
	Chat *chat;
	Chat *chatlast=nil;
	while(chat = [e nextObject]) {
		if([chat type]==ChatTypeRoom) {
			[[[NSApp engine] chatsController] chatWithRoom:[chat room] autojoin:YES];
		}
		else {
			[[[NSApp engine] chatsController] chatWithPeer:[chat peer]];
		}
		chatlast = chat;
	}
	if(chatlast) {
		[NSApp expandSourceListElement:[ChatsElement elementChatsRoot]];
		[NSApp selectSourceListElement:[ChatsElement elementForChat:chatlast]];
	}
	[self saveChats];
}

- (IBAction)leaveRoom:(id)sender
{
	NSEnumerator *e = [[nibControllerConversations selectedObjects] objectEnumerator];
	Chat *chat;
	while(chat = [e nextObject]) {
		[[[NSApp engine] chatsController] endChat:chat];
	}
}

- (void)endChats:(id)sender removeRooms:(BOOL)removeRooms {
	NSEnumerator *e = [[nibControllerConversations selectedObjects] objectEnumerator];
	Chat *chat;
	while(chat = [e nextObject]) {
		if([chat type]==ChatTypeRoom && removeRooms) {
			[[[NSApp engine] chatsController] removeRoomFromList:[chat room]];
		}
		[[[NSApp engine] chatsController] endChat:chat];
	}
	[self saveChats];

}

- (IBAction)removeRoom:(id)sender {
	[self endChats:sender removeRooms:YES];
}

- (IBAction)endChats:(id)sender {
	[self endChats:sender removeRooms:NO];
}
	
- (IBAction)select:(id)sender {
	[NSApp selectSourceListElement:[self element]];
}


- (NSArray *)participantsSelected { return [nibControllerParticipants selectedObjects]; }

- (void)saveChats {
	NSEnumerator *e = [[[[[NSApp engine] chatsController] chatsInside] mutableSet] objectEnumerator];
	Chat *c;
	NSMutableArray *ma = [NSMutableArray array];
	while(c = [e nextObject]) {
		if([c type]==ChatTypeRoom) {
			[ma addObject:[[c room] name]];
		}
	}
	[[NSUserDefaults standardUserDefaults] setObject:ma forKey:SolarSeekDefaultChats];
}
- (IBAction)startPrivateChats:(id)sender
{
	NSEnumerator *e = [[nibControllerParticipants selectedObjects] objectEnumerator];
	if([[[NSApp selectedSourceListElement] view] respondsToSelector:@selector(participantsSelected)])
		e = [[[[NSApp selectedSourceListElement] view] participantsSelected] objectEnumerator];
	else
		e = [[nibControllerParticipantsAll selectedObjects] objectEnumerator];
	Peer *p;
	Chat *chat = nil;
	while(p = [e nextObject]) {
		chat = [[[NSApp engine] chatsController] chatWithPeer:p];
	}
	if(chat) {
		[ChatsElement scheduleSelectChat:chat];
	}
	[self saveChats];
}

- (NSView *)nibViewSingleChat { return nibViewSingleChat; }
- (NSView *)nibViewGroupChat { return nibViewGroupChat; }


- (float)splitView:(NSSplitView *)sender constrainMinCoordinate:(float)proposedMin ofSubviewAt:(int)offset {
	return 100.0;
}

- (float)splitView:(NSSplitView *)sender constrainMaxCoordinate:(float)proposedMin ofSubviewAt:(int)offset {
	return [sender frame].size.width - 250.0;
}

- (void)splitView:(NSSplitView *)sender resizeSubviewsWithOldSize:(NSSize)oldSize {
	NSSize newSize = [sender frame].size;
	
	float delta = newSize.width - oldSize.width;
	
	float thick = [sender dividerThickness];
	
	NSRect left = [[[sender subviews] objectAtIndex:0] frame];
	NSRect right = [[[sender subviews] objectAtIndex:1] frame];
	
	//increase the left one
//	left.size.width += delta;
//	right.origin.x += delta;

	//increase the right one.
	right.size.width += delta;
	
	if(left.size.width < 100) {
		left.size.width = 100;
		right.size.width = newSize.width - left.size.width;
	}
	
	right.size.height = left.size.height = newSize.height;

	[[[sender subviews] objectAtIndex:0] setFrame:left];
	[[[sender subviews] objectAtIndex:1] setFrame:right];
}


@end
